from math import * 
import array
import matplotlib.pyplot as plt
import wave
import numpy as np
 
def sinesweep(f0, f1, sweeptime, samplingrate, peak): 
    k = exp(log(float(f1)/f0)/sweeptime)
    data_len = sweeptime * samplingrate
    data = array.array('i',[0]*data_len)
    dt = 1.0/samplingrate
    t = 0.0 
    p = 2*pi*f0/log(k)
    for i in range(data_len):
        data[data_len-i-1] = int(peak*sin( p*(pow(k,t)-1) ))
        t += dt 
    return data
 
def sindata(f0, time, samplingrate, peak): 
    data = [int(peak*sin(2*pi*f0*x/samplingrate)) for x in range(int(time*samplingrate))] 
    return array.array('h', data)
 
if 1:
    SAMPLING_RATE = 44100 
    SWEEP_TIME = 2
    F0 = 1 
    F1 = 400e+6 
    PEAK = 0x2000 
    data = sinesweep(F0, F1, SWEEP_TIME, SAMPLING_RATE, PEAK) 
    np.savetxt("data_sinesweep.txt",data,fmt='%f',delimiter=',')
    plt.title("sweep data")
    plt.plot(data)
    plt.show()